"""
重返未来1999出卡决策代码
"""
class Card:#创建Card类
    #初始化
    def __init__(self):
        pass
    """
    Bonus_Judgment加分判定函数
    传参说明
        operation_sequence---出卡顺序列表
        card_order---卡牌顺序列表
    返回
        函数会返回加分分值
    """
    def Bonus_Judgment(self,operation_sequence,card_order):
        Bonus_Judgment = []
        for card_count in range(0,len(operation_sequence)):
            owner = [];level = []
            score = 0#  重制分数
            for i in range(0,3):
                owner.append(card_order[card_count][i].split(" ")[1])#卡牌所属人物信息整合
                level.append(card_order[card_count][i].split(" ")[0])#卡牌等级信息整合
            #自然合卡加分
            score = score + card_order[card_count][3]
            #卡牌阶级加分
            for j in range(0,3):
                if level[j] == "2":
                    score  = score + 1
                if level[j] == "3":
                    score == score + 1.5
            #至终仪式加分
            if "a2" in owner or "b2" in owner or "c2" in owner:
                score = score + 3
            #反制卡加分
            if "c0" in owner:
                score = score + 0.5
            #远旅"明珠"卡加分
            if "c1" in owner:
                if int(level[owner.index("c1")]) > 1 and int(level[owner.index("c1")]) != 2:
                    if owner[owner.index("c1")+1] != "c0":
                        score = score + 1
            #槲寄生特殊石化卡位置加分
            if "b0" in owner:
                if int(level[owner.index("b0")]) > 1 and int(owner.index("b0")) == 2:#如果槲寄生的石化卡(卡牌等级大于2)在后面
                    score = score + 1
            Bonus_Judgment.append(score)

        return Bonus_Judgment
    """
    extract_card抽卡函数
    传参说明
        n---要抽掉的卡牌位置(1~7)
        ownership_list---卡牌所属人物列表
        card_level_list---卡牌等级列表
    返回
        ownership_list---抽掉卡牌后的卡牌所属人物列表
        card_level_list---抽掉卡牌后的卡牌等级列表
        push_card--抽掉的卡的信息
        combination_count---抽掉卡后的自然合卡次数(方便后续加分)
    """
    def extract_card(self,n,ownership_list,card_level_list):
        combination_count = 0#合卡次数
        push_card = str(card_level_list[n-1]) +" "+str(ownership_list[n-1])#打出的卡牌信息保存

        ownership_list.pop(n-1);card_level_list.pop(n-1)#删除抽掉的卡牌信息
        list_lenght = len(ownership_list) - 1
        for i in range(1,list_lenght):
            if i == list_lenght:#实时判断
                break
            #因为抽掉卡牌后可能会出现多次合卡情况，需要循环判定，循环条件不成立会继续往下执行后续代码
            while ownership_list[i-1] == ownership_list[i] and card_level_list[i-1] == card_level_list[i]:#卡牌的旁边一项类型和等级相同触发合卡条件为True
                ownership_list.pop(i);card_level_list.pop(i)#自然合卡时会合并，删掉一张卡的信息
                card_level_list[i-1] = card_level_list[i-1] + 1#合并的卡牌等级加一
                list_lenght -= 1
                combination_count += 1#合卡次数+1

        return (ownership_list,card_level_list,push_card,combination_count)

    """
    enumerate_all_possibilities列举所有卡牌组合函数
    传参说明
        ownership_list---最开始识别到的卡牌所属人物列表
        card_level_list---最开始识别到的卡牌等级列表
    返回
        operation_sequence---所有卡牌组合的出牌顺序
        card_order---所有卡牌组合的卡牌顺序
    """
    def enumerate_all_possibilities(self,ownership_list,card_level_list):
        card = Card()
        old_ownership_list = []
        old_card_level_list = []
        operation_sequence = [];card_order = []
        old_ownership_list.append(ownership_list.copy())
        old_card_level_list.append(card_level_list.copy())
        score = 0
        for i in range(1,8):
            information = card.extract_card(i,ownership_list,card_level_list)
            push_card1 = information[2]
            score1 = information[3]

            old_ownership_list.append(ownership_list.copy())
            old_card_level_list.append(card_level_list.copy())
            for j in range(1,len(ownership_list)-1):
                information = card.extract_card(j, ownership_list, card_level_list)
                push_card2 = information[2]
                score2 = information[3]

                old_ownership_list.append(ownership_list.copy())
                old_card_level_list.append(card_level_list.copy())

                for k in range(1,len(ownership_list)-1):
                    information = card.extract_card(k, ownership_list, card_level_list)

                    push_card3 = information[2]
                    score3 = information[3]

                    ownership_list = old_ownership_list[2].copy()
                    card_level_list = old_card_level_list[2].copy()

                    operation_sequence_string = str(i) +" "+ str(j) +" "+ str(k)
                    operation_sequence.append(operation_sequence_string)

                    score_sum = score1+score2+score3
                    card_order.append([push_card1,push_card2,push_card3,score_sum])

                ownership_list = old_ownership_list[1].copy()
                card_level_list = old_card_level_list[1].copy()

            ownership_list = old_ownership_list[0].copy()
            card_level_list = old_card_level_list[0].copy()

        return (operation_sequence,card_order)

    """
    choose_best最佳卡牌组合选择函数
    传参说明
        ownership_list---最开始识别到的卡牌所属人物列表
        card_level_list---最开始识别到的卡牌等级列表
    返回
        函数会返回最佳卡牌组合的出卡顺序
    """
    def choose_best(self,ownership_list,card_level_list):
        score = 0
        #枚举所有成立情况
        card = Card()
        operation_sequence,card_order = card.enumerate_all_possibilities(ownership_list,card_level_list)
        print("判断出",len(operation_sequence),"种情况")
        
        bonus_judgment_score = card.Bonus_Judgment(operation_sequence,card_order)
        
        #遍历循环找分数最大的卡牌组合
        max_score = 0
        max_score_index = 0
        for s in range(0,len(bonus_judgment_score)):
            if bonus_judgment_score[s] >= max_score:
                max_score = bonus_judgment_score[s]
                max_score_index = s
        
        return operation_sequence[max_score_index]
"""
下面的程序为调试decision程序的测试程序，不影响主函数运行，单独运行该文件时才会触发
"""
if __name__ == "__main__":
    ownership_list = ['b0', 'a0', 'c1', 'b0', 'c0', 'a2', 'c0']
    card_level_list = [1, 1, 1, 2, 1, 1, 1]

    print("原列表:",ownership_list)
    print("原等级:",card_level_list);print("-"*37)
    # card = Card()
    # ownership_list,card_level_list = card.extract_card(2,ownership_list,card_level_list)
    # print("-" * 37);print("抽掉第", 2, "张卡牌后:")
    # print(card_level_list);print(ownership_list)
    card = Card()
    operation = card.choose_best(ownership_list,card_level_list)
    print(operation)
    print(operation.split(" ")[0],operation.split(" ")[1],operation.split(" ")[2])
